package com.doubi.controller.sysUser;

import com.doubi.common.aspectj.annotation.SysLog;
import com.doubi.common.config.shiro.ShiroUtil;
import com.doubi.model.sys.TSysDept;
import com.doubi.model.sys.TSysDeptRole;
import com.doubi.model.sys.TSysRole;
import com.doubi.model.sys.TSysUser;
import com.doubi.service.SysDeptRoleService;
import com.doubi.service.SysDeptService;
import com.doubi.service.SysRoleService;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("sysDept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysDeptRoleService sysDeptRoleService;

    /** 
     * 功能描述：部门列表 页面
     * @author   MrDoubi 2019-06-27 17:38
     * @return      返回页面路径
     */

    @RequiresPermissions("sysDept:list")
    @GetMapping("list")
    public String list(){
        return "web/sysDept/list";
    }

    /** 
     * 功能描述：获取部门列表json
     * @author   MrDoubi 2019-06-27 17:50
     * @return      返回AppModel对象
     */
    @SysLog(title = "查询部门列表")
    @RequiresPermissions("sysDept:json")
    @PostMapping("json")
    @ResponseBody
    public AppModel getList(){
        List<TSysDept> sysDepts = sysDeptService.getAllBySortNum();
        return new AppModel(0,"获取成功！",sysDepts);
    }

    /**
     * 功能描述：进入新增或修改部门页面
     * @author   MrDoubi 2019-06-28 09:41
     * @param sysDept       TSysDept对象
     * @param model         Model
     * @return              返回页面路径
     */
    @SysLog(title = "进入新增或修改部门页面")
    @RequiresPermissions(value = {"sysDept:edit", "sysDept:add"}, logical = Logical.OR)
    @GetMapping("edit")
    public String edit(TSysDept sysDept, Model model) {
        if (DataUtil.isNotEmpty(sysDept.getOid())) {
            sysDept = sysDeptService.getById(sysDept.getOid());
            TSysDeptRole sysDeptRole = sysDeptRoleService.getBeanByDeptOid(sysDept.getOid());
            model.addAttribute("sysDeptRole", sysDeptRole);
        }
        model.addAttribute("bean", sysDept);
        List<TSysRole> sysRoleList = sysRoleService.list();
        model.addAttribute("sysRoleList", sysRoleList);
        return "web/sysDept/edit";
    }

    /**
     * 功能描述：启用禁用部门
     * @author   MrDoubi 2019-06-28 11:05
     * @param oid           部门oid
     * @return              返回AppModel对象
     */
    @SysLog(title = "启用禁用部门")
    @RequiresPermissions("sysDept:updateStatus")
    @PostMapping("updateStatus")
    @ResponseBody
    public AppModel updateStatus(String oid) {
        TSysUser sysUser = ShiroUtil.getUser();
        return new AppModel(0, "数据操作成功！", sysDeptService.updateStatus(oid, sysUser));
    }




    /**
     * 功能描述：选择部门列表 页面
     * @author   MrDoubi 2019-06-27 17:38
     * @return      返回页面路径
     */
    @RequiresPermissions("sysDept:selectList")
    @GetMapping("selectList")
    public String selectList(Model model, String deptOid){
        model.addAttribute("deptOid",deptOid);
        return "web/sysDept/selectList";
    }
}
